Page({
  data: {
    searchValue: '',
    categories: [
      { id: 1, name: '账号问题', icon: 'user' },
      { id: 2, name: '渔船管理', icon: 'boat' },
      { id: 3, name: '捕捞记录', icon: 'fish' },
      { id: 4, name: '二维码', icon: 'qrcode' },
      { id: 5, name: '企业认证', icon: 'company' },
      { id: 6, name: '其他问题', icon: 'more' }
    ],
    questions: []
  },

  onLoad() {
    this.loadQuestions();
  },

  onSearchChange(e) {
    this.setData({
      searchValue: e.detail.value
    });
    this.searchQuestions(e.detail.value);
  },

  loadQuestions() {
    wx.request({
      url: 'https://api.example.com/help/questions',
      method: 'GET',
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            questions: res.data.map(item => ({
              ...item,
              showAnswer: false
            }))
          });
        } else {
          wx.showToast({
            title: '获取问题列表失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  searchQuestions(keyword) {
    wx.request({
      url: `https://api.example.com/help/search?keyword=${keyword}`,
      method: 'GET',
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            questions: res.data.map(item => ({
              ...item,
              showAnswer: false
            }))
          });
        } else {
          wx.showToast({
            title: '搜索失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  selectCategory(e) {
    const categoryId = e.currentTarget.dataset.id;
    wx.request({
      url: `https://api.example.com/help/category/${categoryId}`,
      method: 'GET',
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            questions: res.data.map(item => ({
              ...item,
              showAnswer: false
            }))
          });
        } else {
          wx.showToast({
            title: '获取问题列表失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  showAnswer(e) {
    const questionId = e.currentTarget.dataset.id;
    const { questions } = this.data;
    const index = questions.findIndex(item => item.id === questionId);
    if (index !== -1) {
      questions[index].showAnswer = !questions[index].showAnswer;
      this.setData({ questions });
    }
  },

  goBack() {
    wx.navigateBack();
  }
}); 